package com.usian.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usian.common.dtos.PageResult;
import com.usian.common.enums.AppHttpCodeEnum;
import com.usian.common.exception.LeadException;
import com.usian.common.util.BeanHelper;
import com.usian.model.user.dtos.AuthDto;
import com.usian.model.user.dtos.UserAuthDto;
import com.usian.user.entity.ApUserAuth;
import com.usian.user.mapper.ApUserAuthMapper;
import com.usian.user.service.IApUserAuthService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 * APP实名认证信息表 服务实现类
 * </p>
 *
 * @author usian
 * @since 2022-07-05
 */
@Service
public class ApUserAuthServiceImpl extends ServiceImpl<ApUserAuthMapper, ApUserAuth> implements IApUserAuthService {

    @Override
    public PageResult<UserAuthDto> findByPage(AuthDto dto) {
        if(dto==null){
            throw new LeadException(AppHttpCodeEnum.PARAM_INVALID);
        }
        //1、构建分页查询条件
        Page<ApUserAuth> page = new Page<>(dto.getPage(),dto.getSize());
        //2、构建敏感词名称模糊查询
        QueryWrapper<ApUserAuth> queryWrapper = new QueryWrapper<>();
        if(dto.getStatus()!=null){
            queryWrapper.lambda().eq(ApUserAuth::getStatus,dto.getStatus());
        }
        //3、执行查询
        IPage<ApUserAuth> iPage = this.page(page, queryWrapper);
        //4、封装查询结果
        if(CollectionUtils.isEmpty(iPage.getRecords())){
            throw new LeadException(AppHttpCodeEnum.DATA_NOT_EXIST,"审核的用户不存在");
        }
        //数据转换
        List<UserAuthDto> authDtoList = BeanHelper.copyWithCollection(iPage.getRecords(), UserAuthDto.class);

        return new PageResult<UserAuthDto>(dto.getPage(),dto.getSize(),iPage.getTotal(),authDtoList);
    }
}
